/**
 * Copyright 2018 
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 * <p>
 * http://www.apache.org/licenses/LICENSE-2.0
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package com.dream.inspect.modules.app.controller;


import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Map;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dream.inspect.common.utils.R;
import com.dream.inspect.common.utils.StringUtil;
import com.dream.inspect.modules.app.constants.AttrConstants;
import com.dream.inspect.modules.sys.common.LoginCommon;
import com.dream.inspect.modules.sys.shiro.ShiroUtils;
import com.google.code.kaptcha.Constants;
import com.google.code.kaptcha.Producer;

/**
 * app登录相关
 * 
 * @author panyc
 */
@RestController
@RequestMapping("/app/")
public class AppLoginController{
	@Autowired
	private Producer producer;
	@Autowired
	private LoginCommon loginCommon;
	
	@RequestMapping("captcha.jpg")
	public void captcha(HttpServletResponse response)throws IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        //生成文字验证码
        String text = producer.createText();
        //生成图片验证码
        BufferedImage image = producer.createImage(text);
        //保存到shiro session
        ShiroUtils.setSessionAttribute(Constants.KAPTCHA_SESSION_KEY, text);
        
        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
	}
	
	/**
	 * 登录
	 */
	@RequestMapping(value = "login")
	public R login(String username, String password, String captcha) {
		/*String kaptcha = ShiroUtils.getKaptcha(Constants.KAPTCHA_SESSION_KEY);
		if(!captcha.equalsIgnoreCase(kaptcha)){
			return R.error("验证码不正确");
		}*/
		return loginCommon.login(username, password, AttrConstants.PSTORE);
	}
	
	/**
	 * 退出
	 */
	@RequestMapping(value = "logout")
	public R logout() {
		try{
			ShiroUtils.logout();
			return R.ok();
		}catch (Exception e){
			return R.error("账户退出失败");
		}
	}
	
	/**
	 * 申请激活
	 * @param params
	 * @return
	 */
	@RequestMapping("applyActivation")
	public R applyActivation(@RequestParam Map<String, Object> params){
		String roleId = params.get("roleId") + "";
		if(StringUtil.isEmpty(roleId)){
			return R.error("请选择需要激活的角色");
		}
		String userId = params.get("userId") + "";
		if(StringUtil.isEmpty(userId)){
			return R.error("请选择需要激活的用户");
		}
		//params.put("userId", getUserId());
//		params.put("userId", params.get("userId") + "");
		return loginCommon.applyActivation(params);
	}
	
	/**
	 * 获取需要可激活的角色列表
	 * @param params
	 * @return
	 */
	@RequestMapping("getRoleInfo")
	public R getRoleInfo(@RequestParam Map<String, Object> params){
		String roleId = params.get("roleId") + "";
		String roleName = params.get("roleName") + "";
		if(StringUtil.isEmpty(roleId) || StringUtil.isEmpty(roleName)){
			return R.error("角色ID或key不能为空");
		}
		return loginCommon.getRoleInfo(params);
	}
	
}
